<!--
 @description: 创建专题表单
 @Author: chich
 @Date: 2021-04-26
-->
<template>
  <div>
    <edit-form :model="form" :fields="formFields" :rules="formRules" v-bind="formAttrs" ref="addForm"></edit-form>
    <div v-footer>
      <dg-button @click="handleCancel">关闭</dg-button>
      <dg-button type="primary" @click="handlePublish" :loading="loading">发布</dg-button>
      <dg-button type="primary" @click="handleSave('0')" :loading="loading">保存</dg-button>
    </div>
  </div>
</template>
<script>
import EditForm from '@@/edit-form';
import { mapState } from 'vuex';
import { subject } from '@/api/thematic';
import { getExists } from '@/api/common';

export default {
  name: 'add-form',
  components: { EditForm },
  props: {
    row: {
      type: Object
    }
  },
  data() {
    return {
      loading: false,
      // 表单配置
      form: {
        title: '',
        zw: ''
      },
      formAttrs: {
        labelWidth: '90px'
      },
      formFields: [
        {
          prop: 'title',
          label: '标题',
          col: 24,
          tag: 'el-input',
          tagBind: {
            placeholder: '请输入标题',
            maxlength: '200',
            showWordLimit: true
          }
        },
        {
          prop: 'zw',
          label: '正文',
          tag: 'el-input',
          tagBind: {
            type: 'textarea',
            rows: 3,
            placeholder: '请输入正文内容',
            maxlength: '2000',
            showWordLimit: true
          }
        }
      ],
      formRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        zw: [{ required: true, message: '请输入正文内容', trigger: 'blur' }]
      }
    };
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    handleCancel() {
      this.$emit('close');
    },
    handleSave(isRelease) {
      this.$refs.addForm.$refs.form.validate((valid) => {
        if (valid) {
          let params = {
            id: this.row && this.row.id,
            ...this.form,
            isRelease
          };
          this.loading = true;
          // 调用接口查看标题是否重复
          getExists({
            searchCondition : JSON.stringify([{name:"title",op:"=",value:this.form.title,type:'',format:''}])
          }).then((res) => {
            if (res.data) {
              this.$message.error('标题重复，无法创建！');
              this.loading = false;
              return;
            } else {
              subject(params)
                .then((data) => {
                  if (data) {
                    this.$emit('success');
                    this.$emit('close');
                  }
                  this.loading = false;
                })
                .catch((err) => {
                  // if (err.response.data.statusCode === '200000') {
                  //   this.$message.error('标题重复，无法创建！');
                  //   this.loading = false;
                  //   return;
                  // }
                  this.loading = false;
                  this.$message.error('保存失败！');
                });
            }
          });
        }
      });
    },
    handlePublish() {
      this.handleSave('1');
    }
  },
  created() {
    if (this.row) {
      Object.keys(this.form).forEach((key) => {
        this.form[key] = this.row[key];
      });
    }
  },
  mounted() {}
};
</script>
<style scoped lang="scss">
.flex-box {
  display: flex;
  align-items: flex-start;
  &__radio {
    margin-left: 1rem;
  }
}
</style>
